var express = require('express');
var router = express.Router();
var db = require("../mysql/index.js");
/* GET users listing. */
router.get('/', function (req, res, next) {
  var sql = "select * from myview where 1 = 1";
  var getcount = "select count(*) as size from myview where 1 = 1"


  if(req.query.uids){
    sql = sql + ` and uid in (${req.query.uids})`
    getcount = getcount + ` and uid in (${req.query.uids})`
  }

  if(req.query.startDate&&req.query.endDate){
    sql = sql + ` and ((start_date>='${req.query.startDate}' and start_date<='${req.query.endDate}')  or (due_date>='${req.query.startDate}'  and due_date<='${req.query.endDate}'))`
    getcount = getcount +  ` and ((start_date>='${req.query.startDate}' and start_date<='${req.query.startDate}')  or (due_date>='${req.query.startDate}'  and due_date<='${req.query.endDate}'))`
   
  }
  
  var pageSize = parseInt(req.query.pageSize)  || 30;
  var pageNum = parseInt(req.query.pageNum)|| 0;
  var offset = pageNum*pageSize
  sql = sql + ` limit ${offset},${pageSize}`
  console.log(sql)
  var webdata = {
    errCode:0,
    errmsg:"",
    total: 0,
    pageSize: 0,
    pageNum: 0,
    data: []
  }

  db.query(getcount).then(data1 => {
    var size  = data1.data[0]['size'];
    webdata.total = size
    db.query(sql).then(data2 => {
      webdata.pageSize = pageSize;
      webdata.pageNum = pageNum;
      webdata.data = data2.data;
      res.send(webdata)
    })
  }).catch(err => {
    res.send({
      errCode:-1,
      errmsg:err.errmsg
    })
  })
});



module.exports = router;